<?php require('connections/con_gl.php'); ?>
<?php
cekAkses($_SESSION[akses],"L-3");
$mulai = $_POST['Mulai'];
if($_POST['Sampai']=='Sampai...') {
	$sampai = $_POST['Mulai'];
}else{
$sampai = $_POST['Sampai'];
}
$currentPage = $_SERVER["PHP_SELF"];

$where_noref ="";

mysql_select_db($database_con_gl, $con_gl);
$query_data = "SELECT * FROM gl_rtrans";
if($mulai <>'Mulai...') {
	if(!strstr($query_data," where ")) {
		$query_data .=" where ";	
	}
	$query_data .=" tgl between '$mulai' AND '$sampai'";
} 
if(!strstr($_SESSION[akses],'A-1-4')) {
	if(!strstr($query_data," where ")) {
		$query_data .= " where ";	
	}
	if($mulai <>'Mulai...') {
		$query_data .= " AND ";	
	}
	$query_data .= "gudang='$_SESSION[lokasi]'";
}
$query_data .=" ORDER BY gl_rtrans.tgl desc";
$data = mysql_query($query_data, $con_gl) or die(mysql_error());
$row_data = mysql_fetch_assoc($data);
$totalRows_data = mysql_num_rows($data);
$rt = 0;
do {
	$rt++;
	$where_noref .= "gl_trans.no_ref='$row_data[no_ref]'";
	if($rt < $totalRows_data) { $where_noref .=" OR ";}
	} 
while ($row_data = mysql_fetch_assoc($data));

mysql_select_db($database_con_gl, $con_gl);
$query_period = "SELECT id, awal, akhir, saldoawal FROM gl_periode ORDER BY id ASC";
$period = mysql_query($query_period, $con_gl) or die(mysql_error());
$row_period = mysql_fetch_assoc($period);
$totalRows_period = mysql_num_rows($period);
?>
<link href="css/ui.datepicker.css" type="text/css" rel="stylesheet" />
<!--<script language="JavaScript" src="js/FusionCharts.js"></script>
<script type="text/javascript" src="calendar/jquery-1.2.3.js"></script>-->
<script type="text/javascript" src="js/ui.datepicker.js"></script>
<script type="text/javascript">
<!--
$(function()
      {
        $('.calendar').datepicker({
            appendText : "",
            dateFormat : 'yy/mm/dd'
          });
      });
</script>
<h1>NERACA  PERUSAHAAN</h1> <?php if(isset($_POST[Search]) AND $_POST[Mulai]<>'Mulai...' AND $_POST[Sampai]<>'Sampai...') { ?><?php tanggal($_POST[Mulai],"tampilkan");?> - <?php tanggal($_POST[Sampai],"tampilkan");?><?php } ?>
<table width="100%" border="0" align="center" cellpadding="5" cellspacing="0"> 
  <tr valign="top" class="hide">
    <td align="right"><form id="search" name="search" method="post" action="">
      <label>
        <input name="Mulai" type="text" class="calendar" id="Mulai" value="<?php if(empty($mulai)) echo "Mulai...";else echo $mulai;?>" onblur="if(this.value=='') this.value='Mulai...';" onfocus="if(this.value=='Mulai...') this.value='';"></label>
      s/d
      <label><input name="Sampai" type="text" class="calendar" id="Sampai" value="<?php if(empty($sampai)) echo "Sampai...";else echo $sampai;?>" onblur="if(this.value=='') this.value='Sampai...';" onfocus="if(this.value=='Sampai...') this.value='';"></label>
      <label>
        <input name="Search" type="submit" id="Search" value="Tampilkan" />
        </label>
    </form>    </td>
  </tr>
  <?php if(isset($_POST['Search'])) { ?>
  <?php if($totalRows_data > 0) { if($_GET['act']=='periode') { $mulai = $row_data['awal'];$sampai = $row_data['akhir'];}?>
  <?php
mysql_select_db($database_con_gl, $con_gl);
$query_getklasaktiva = "SELECT kd, klasifikasi FROM gl_klas WHERE neraca = 1 ORDER BY klasifikasi ASC";
$getklasaktiva = mysql_query($query_getklasaktiva, $con_gl) or die(mysql_error());
$row_getklasaktiva = mysql_fetch_assoc($getklasaktiva);
$totalRows_getklasaktiva = mysql_num_rows($getklasaktiva);

mysql_select_db($database_con_gl, $con_gl);
$query_getklaspasiva = "SELECT kd, klasifikasi FROM gl_klas WHERE neraca = 2 ORDER BY klasifikasi ASC";
$getklaspasiva = mysql_query($query_getklaspasiva, $con_gl) or die(mysql_error());
$row_getklaspasiva = mysql_fetch_assoc($getklaspasiva);
$totalRows_getklaspasiva = mysql_num_rows($getklaspasiva);

mysql_select_db($database_con_gl, $con_gl);
$query_tmp_p = "SELECT gl_klas.klasifikasi, gl_akun.id, gl_akun.akun, gl_trans.total FROM gl_klas, gl_akun, gl_trans WHERE gl_klas.neraca=2 AND gl_akun.klasifikasi=gl_klas.kd AND gl_trans.akun=gl_akun.id AND gl_trans.pos='D'";
$tmp_p = mysql_query($query_tmp_p, $con_gl) or die(mysql_error());
$row_tmp_p = mysql_fetch_assoc($tmp_p);
$totalRows_tmp_p = mysql_num_rows($tmp_p);
$totpp = 0; do {
$totpp += $row_tmp_p['total'];
}while ($row_tmp_p = mysql_fetch_assoc($tmp_p));
?>

  <tr>
    <td >&nbsp;</td>
  </tr>
  <tr>
  
  <td ><table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border-left:solid 1px #DDDDDD;border-right:solid 1px #DDDDDD;">
      <tr valign="top">
        <th height="35" align="center" valign="middle" style="background:#1a4d80;color:#FFF;">AKTIVA</th>
        <th height="35">&nbsp;</th>
        <th height="35" align="center" valign="middle" style="background:#1a4d80;color:#FFF;">PASIVA</th>
      </tr>
    <tr valign="top">
    
    <td width="51%"><table width="100%" border="0" cellspacing="0" cellpadding="5" class="datatable">
      <?php $totaktiva = 0; do { ?>
	  <?php
	  mysql_select_db($database_con_gl, $con_gl);
$query_getakun_a = "SELECT id, akun FROM gl_akun where klasifikasi ='$row_getklasaktiva[kd]' ORDER BY akun ASC";
$getakun_a = mysql_query($query_getakun_a, $con_gl) or die(mysql_error());
$row_getakun_a = mysql_fetch_assoc($getakun_a);
$totalRows_getakun_a = mysql_num_rows($getakun_a);
	  ?>
        <tr>
          <td colspan="2"><span style="font-weight: bold"><?php echo $row_getklasaktiva['klasifikasi']; ?></span></td>
        </tr>
		<?php $totklas_a = 0; do { ?>
		<?php
		mysql_select_db($database_con_gl, $con_gl);
$query_trans_ak = "SELECT sum(gl_trans.total) FROM gl_trans WHERE (".$where_noref.") AND gl_trans.pos='D' AND gl_trans.akun = '$row_getakun_a[id]'";
$trans_ak = mysql_query($query_trans_ak, $con_gl) or die(mysql_error());
$row_trans_ak = mysql_fetch_assoc($trans_ak);
$totalRows_trans_ak = mysql_num_rows($trans_ak);

mysql_select_db($database_con_gl, $con_gl);
$query_trans_ak2 = "SELECT sum(gl_trans.total) FROM gl_trans WHERE (".$where_noref.") AND gl_trans.pos='K' AND gl_trans.akun = '$row_getakun_a[id]'";
$trans_ak2 = mysql_query($query_trans_ak2, $con_gl) or die(mysql_error());
$row_trans_ak2 = mysql_fetch_assoc($trans_ak2);
$totalRows_trans_ak2 = mysql_num_rows($trans_ak2);
$nil1 = $row_trans_ak['sum(gl_trans.total)'] - $row_trans_ak2['sum(gl_trans.total)'];
		?>
        <tr>
          <td width="63%" style="padding-left:18px;"><?php echo $row_getakun_a['akun']; ?></td>
          <td width="37%" align="right"><?php if($nil1 >= 0) { echo number_format($nil1,0,',','.').",-";}else{ echo "( ".number_format(str_replace("-","",$nil1),0,',','.').",-"." )";} $totklas_a += $nil1;?></td>
        </tr>
		<?php } while ($row_getakun_a = mysql_fetch_assoc($getakun_a)); ?>
      <tr>
        <td align="right" style="border-bottom:solid 1px #DDDDDD;">&nbsp;</td>
        <td align="right" bgcolor="#EEEEEE" style="border-bottom:solid 1px #DDDDDD;"><?php if($totklas_a >= 0) { echo number_format($totklas_a,0,',','.').",-";}else{ echo "( ".number_format(str_replace("-","",$totklas_a),0,',','.').",-"." )";} $totaktiva += $totklas_a;?></td>
      </tr>
	  <?php } while ($row_getklasaktiva = mysql_fetch_assoc($getklasaktiva)); ?>
	   <?php $koreksi_a = $totpp - $totaktiva; if($koreksi_a > 0) { ?>
      <tr>
        <td>&nbsp;</td>
        <td align="right">&nbsp;</td>
      </tr>
      <tr>
        <td>Selisih : </td>
        <td align="right" bgcolor="#FFFF99"><?php echo number_format($koreksi_a,0,',','.').",-";$totaktiva += $koreksi_a;?></td>
      </tr>
	  <?php } ?>
      <tr>
        <td>&nbsp;</td>
        <td align="right">&nbsp;</td>
      </tr>
      <tr>
        <td align="right">Total Aktiva : </td>
        <td align="right" bgcolor="#EEEEEE"><?php if($totaktiva >=0) { echo number_format($totaktiva,0,',','.').",-";}else{ echo "( ".number_format(str_replace("-","",$totaktiva),0,',','.').",-"." )";}?></td>
      </tr>
    </table></td>
    <td width="5%" bgcolor="#DDDDDD" style="border:solid 1px #DDDDDD;">&nbsp;</td>
    <td width="47%"><table width="100%" border="0" cellspacing="0" cellpadding="5" class="datatable">
      <?php $totpasiva = 0; do { ?>
	  <?php
	  mysql_select_db($database_con_gl, $con_gl);
$query_getakun_p = "SELECT id, akun FROM gl_akun where klasifikasi ='$row_getklaspasiva[kd]' ORDER BY akun ASC";
$getakun_p = mysql_query($query_getakun_p, $con_gl) or die(mysql_error());
$row_getakun_p = mysql_fetch_assoc($getakun_p);
$totalRows_getakun_p = mysql_num_rows($getakun_p);
	  ?>
	  <tr>
        <td colspan="2"><span style="font-weight: bold"><?php echo $row_getklaspasiva['klasifikasi']; ?></span></td>
      </tr>
	  <?php $totklas_p = 0; do { ?>
	  <?php
	  mysql_select_db($database_con_gl, $con_gl);
$query_trans_pas = "SELECT sum(gl_trans.total) FROM gl_trans WHERE (".$where_noref.") AND gl_trans.pos='D' AND gl_trans.akun = '$row_getakun_p[id]'";
$trans_pas = mysql_query($query_trans_pas, $con_gl) or die(mysql_error());
$row_trans_pas = mysql_fetch_assoc($trans_pas);
$totalRows_trans_pas = mysql_num_rows($trans_pas);

mysql_select_db($database_con_gl, $con_gl);
$query_trans_pas2 = "SELECT sum(gl_trans.total) FROM gl_trans WHERE (".$where_noref.") AND gl_trans.pos='K' AND gl_trans.akun = '$row_getakun_p[id]'";
$trans_pas2 = mysql_query($query_trans_pas2, $con_gl) or die(mysql_error());
$row_trans_pas2 = mysql_fetch_assoc($trans_pas2);
$totalRows_trans_pas2 = mysql_num_rows($trans_pas2);
$nil2 = $row_trans_pas['sum(gl_trans.total)'] - $row_trans_pas2['sum(gl_trans.total)'];
	  ?>
      <tr>
        <td width="63%" style="padding-left:18px;"><?php echo $row_getakun_p['akun']; ?></td>
        <td width="37%" align="right"><?php if($nil >=0) { echo number_format($nil2,0,',','.').",-";}else{ echo "( ".number_format(str_replace("-","",$nil2),0,',','.').",-"." )";} $totklas_p += $nil2;?></td>
      </tr>
	  <?php } while ($row_getakun_p = mysql_fetch_assoc($getakun_p)); ?>
      <tr>
        <td align="right" style="border-bottom:solid 1px #DDDDDD;">&nbsp;</td>
        <td align="right" bgcolor="#EEEEEE" style="border-bottom:solid 1px #DDDDDD;"><?php if($totklas_p >=0) { echo number_format($totklas_p,0,',','.').",-";}else{ echo "( ".number_format(str_replace("-","",$totklas_p),0,',','.').",-"." )";} $totpasiva += $totklas_p;?></td>
      </tr>
	  <?php } while ($row_getklaspasiva = mysql_fetch_assoc($getklaspasiva)); ?>
      <?php $koreksi_p = $totaktiva - $totpasiva; if($koreksi_p > 0) { ?>
      <tr>
        <td>&nbsp;</td>
        <td align="right">&nbsp;</td>
      </tr>
      <tr>
        <td align="right">Selisih : </td>
        <td align="right" bgcolor="#FFFF99"><?php echo number_format($koreksi_p,0,',','.').",-";$totpasiva += $koreksi_p;?></td>
      </tr>
	  <?php } ?>
        <td>&nbsp;</td>
        <td align="right">&nbsp;</td>
      </tr>
      <tr>
        <td align="right">Total Pasiva : </td>
        <td align="right" bgcolor="#EEEEEE"><?php if($totpasiva >=0) { echo number_format($totpasiva,0,',','.').",-";}else{ echo "( ".number_format(str_replace("-","",$totpasiva),0,',','.').",-"." )";}?></td>
      </tr>
    </table></td>
    </tr>
    
  </table></td>
  </tr>
  
  <?php }else{ ?>
  <tr>
    <td>Data tidak ada !!! </td>
  </tr>
  <?php } ?>
  <?php } ?>
  <tr>
    <td></td>
  </tr>
</table>
